#include "timer.h"

__IO uint32_t g_localtime = 0; /* for creating a time reference incremented by 10ms */
uint16_t heart_beat_send = 0;
uint16_t tim1=0;
uint16_t tim2=0;

void set_gpio()
{
	gpio_bit_set(GPIOA, GPIO_PIN_4);
}

void reset_gpio()
{
	gpio_bit_reset(GPIOA, GPIO_PIN_4);
}

void timer1_config(void)
{
	timer_parameter_struct timer_s;
	rcu_periph_clock_enable(RCU_TIMER1);
	timer_s.period = 10000-1;
	timer_s.clockdivision = TIMER_CKDIV_DIV1;
	timer_s.prescaler = 1000;
	timer_s.counterdirection = TIMER_COUNTER_UP;
	timer_s.alignedmode = TIMER_COUNTER_EDGE;
	timer_init(TIMER1,&timer_s);
	timer_interrupt_flag_clear(TIMER1,TIMER_INT_UP);
	timer_interrupt_enable(TIMER1, TIMER_INT_UP);
	timer_enable(TIMER1);
	nvic_irq_enable(TIMER1_IRQn, 1, 2);
	//tim1=11;
}


void TIMER1_IRQHandler()
{
	if (timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET){
		tim1++;
		timer_interrupt_flag_clear(TIMER1,TIMER_INT_UP);
		//usart_data_transmit(USART5,tim1);
	}
}
void timer2_config(void)
{
	timer_parameter_struct timer_s;
	rcu_periph_clock_enable(RCU_TIMER2);
	timer_s.period = 10000-1;
	timer_s.clockdivision = TIMER_CKDIV_DIV1;
	timer_s.prescaler = 10000;
	timer_s.counterdirection = TIMER_COUNTER_UP;
	timer_s.alignedmode = TIMER_COUNTER_EDGE;
	timer_init(TIMER2,&timer_s);
	timer_interrupt_flag_clear(TIMER2,TIMER_INT_UP);
	timer_interrupt_enable(TIMER2, TIMER_INT_UP);
	timer_enable(TIMER2);
	nvic_irq_enable(TIMER2_IRQn, 1, 2);
}


void TIMER2_IRQHandler()
{
	if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET){
		tim2++;
		//usart_data_transmit(USART5,tim2);
		timer_interrupt_flag_clear(TIMER2,TIMER_INT_UP);
	}
	//tim2 =12;
	
}
void timer8_config(void)
{
	timer_parameter_struct timer_s;
	rcu_periph_clock_enable(RCU_TIMER0);
	rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
	timer_deinit(TIMER0);
	
	timer_s.prescaler         = 199;
	timer_s.alignedmode       = TIMER_COUNTER_EDGE;
	timer_s.counterdirection  = TIMER_COUNTER_UP;
	timer_s.period            = 599;
	timer_s.clockdivision     = TIMER_CKDIV_DIV1;
	timer_s.repetitioncounter = 0;
	timer_init(TIMER0,&timer_s);
	timer_interrupt_flag_clear(TIMER0,TIMER_INT_UP);
	timer_interrupt_enable(TIMER0, TIMER_INT_UP);
	timer_enable(TIMER0);
	nvic_irq_enable(TIMER0_TRG_CMT_TIMER10_IRQn, 0, 1);
}


void TIMER8_IRQHandler()
{
	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_UP) != RESET){
		timer_interrupt_flag_clear(TIMER0,TIMER_INT_UP);
		usart_data_transmit(USART5,0xFE);
	}
	//usart_data_transmit(USART5,0xF0);
	
	
}
//void usart_config()
//{
//	/* USART5 interrupt configuration */
//	nvic_irq_enable(USART5_IRQn, 0, 0);
//	rcu_periph_clock_enable(RCU_GPIOC);    
//	rcu_periph_clock_enable(RCU_USART5);
//	/* connect port to USART5_Tx */
//	gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_6);
//	/* connect port to USART5_Rx */
//	gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_7);
//	/* configure USART5 Tx as alternate function push-pull */
//	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
//	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
//	/* configure USART5 Rx as alternate function push-pull */
//	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
//	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
//	/* USART5 configure */
//	usart_deinit(USART5);
//	usart_baudrate_set(USART5,115200U);
//	usart_parity_config(USART5,USART_PM_NONE);
//	usart_word_length_set(USART5,USART_WL_8BIT);
//	usart_stop_bit_set(USART5,USART_STB_1BIT);
//	usart_receive_config(USART5, USART_RECEIVE_ENABLE);
//	usart_transmit_config(USART5, USART_TRANSMIT_ENABLE);
//	/* enable USART5 receive interrupt */
//	usart_interrupt_enable(USART5, USART_INTEN_RBNEIE);
//	usart_interrupt_enable(USART5, USART_INTEN_IDLEIE);
//	/* enable USART5 */
//	usart_enable(USART5);
//}
